package reflection.class_;

import reflection.Cat;

/**
 * Created with IntelliJ IDEA
 * Description:6种获得Class对象的方法
 * User:22954
 * Date:2024-04-14
 * Time:19:27
 */
public class GetClass {
    /*
    Class.forName()
    类名.class
    对象.getClass()
    ClassLoader
    基本数据类型.class
    基本数据类型对应包装类.TYPE
     */
    public static void main(String[] args) throws ClassNotFoundException {
        //在编译时期获得类对象,一般与配置文件交互
        String classFullPath = "reflection.Cat";//类的全路径,一般由配置文件获得
        Class<?> cls1 = Class.forName(classFullPath);
        System.out.println(cls1);//class reflection.Cat
        //在加载时期获得类对象,一般用于参数传递
        Class<Cat> cls2 = Cat.class;
        System.out.println(cls2);//class reflection.Cat
        //在运行时期获得类对象,用于已知实例化对象
        Cat cat = new Cat();
        Class<? extends Cat> cls3 = cat.getClass();
        System.out.println(cls3);//class reflection.Cat
        //先获得类加载器,再通过类加载器获得Class对象
        ClassLoader classLoader = cat.getClass().getClassLoader();
        Class<?> cls4 = classLoader.loadClass(classFullPath);
        System.out.println(cls4);

        System.out.println(cls1.hashCode());//189568618
        System.out.println(cls2.hashCode());//189568618
        System.out.println(cls3.hashCode());//189568618
        System.out.println(cls4.hashCode());//189568618
        //基本数据类型.class
        Class<Integer> cls5 = int.class;
        System.out.println(cls5);//int
        //基本数据类型对应包装类.TYPE
        Class<Integer> cls6 = Integer.TYPE;
        System.out.println(cls6);//int

        System.out.println(cls5.hashCode());//1735600054
        System.out.println(cls6.hashCode());//1735600054
    }
}
